package com.bigdata.core.transformations

import org.apache.spark.rdd.RDD
import org.apache.spark.{SparkConf, SparkContext}

/**
 * groupBy 按照指定的规则，将数据分组
 */
object Demo21_groupBy {
  def main(args: Array[String]): Unit = {
    val conf = new SparkConf().setMaster("local").setAppName("groupBy")
    val sc = new SparkContext(conf)
    sc.setLogLevel("error")

    // (name, score)
    val rdd = sc.parallelize(List[(String, Double)](("zhangsan", 66.5),
      ("lisi", 33.2), ("zhangsan", 66.7), ("lisi", 33.4), ("zhanglong", 60),
      ("zhangsan", 66.8), ("wangwu", 29.8)))

    // 按照 name 分组
    //    val result: RDD[(String, Iterable[(String, Double)])] = rdd.groupBy(
    //      (data: (String, Double)) => {
    //        data._1
    //      }
    //    )

    // 按照是否及格分组
//    val result: RDD[(String, Iterable[(String, Double)])] = rdd.groupBy(data => {
//      if (data._2 >= 60) {
//        "及格"
//      } else {
//        "不及格"
//      }
//    })

    // 按照是否姓 张 分组, 姓 张 为true ,不姓张为false
    val result: RDD[(Boolean, Iterable[(String, Double)])] = rdd.groupBy(kv => {
      if (kv._1.contains("zhang")) {
        true
      } else {
        false
      }
    })

    result.foreach(println)
  }
}
